use anyhow::{Result, Context};
use db::{Db};
use matrix_sdk::{
    room::Joined,
    ruma::events::room::message::{
        OriginalSyncRoomMessageEvent, Relation, RoomMessageEventContent,
    },
    Client,
};
use uuid::Uuid;
use std::ops::Deref;

pub async fn show_real_url(
    ev: OriginalSyncRoomMessageEvent,
    _text: String,
    room: Joined,
    _client: Client,
    db: Db,
) -> Result<()> {
    let relation = ev.content.relates_to.unwrap();
    if let Relation::Reply { in_reply_to } = relation {
        let event_id = in_reply_to.event_id.to_string();
        if let Some(userpost_id) = db.get_userpost_id(event_id).await? {
            let real_url = db.get_real_url(userpost_id).await?;
            if let Some(real_url) = real_url{
            // (row.get(0), row.get(1)) 
            let content = RoomMessageEventContent::text_markdown(format!("real url: {}", real_url));
            let response = room.send(content, None).await.unwrap();
    let event_id = response.event_id.to_string();
    db.create_message_event(userpost_id,  event_id).await.unwrap();
}
            }
            log::info!("toggled show_alternative_url on post");
        // }
        }
        Ok(())
    }